package com.zhoug.zmui.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.ZMUILoadView;
import com.zhoug.zmui.utils.ZMUIUtils;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

/**
 * 提示dialog
 * zmui_style_ZMUITipDialog
 *
 * @Author 35574
 * @Date 2020/8/10
 * @Description
 */
public class ZMUITipDialog extends ZMUIDialog {
    private static final String TAG = ">>>ZMUITipDialog";
    private static final boolean DEBUG = false;
    public static final int TYPE_LOADING = 1;//正在加载
    public static final int TYPE_SUCCESS = 2;//成功
    public static final int TYPE_ERROR = 3;//错误
    public static final int TYPE_INFO = 4;//信息
    private int type;//提示图标类型,0不显示图标
    private String info;//提示文本内容
    private LinearLayout mTipContent;
    private int iconSize;//图标大小
    private float textSize;//文字大小
    private int textColor;//文字颜色
    private @DrawableRes
    int background;//窗口背景
    private int paddingH;//窗口水平边距
    private int paddingV;//窗口垂直边距

    public static final int SHOW_LENGTH_SHORT = 1000;
    public static final int SHOW_LENGTH_LONG = 2000;
    private int showLength = SHOW_LENGTH_SHORT;//显示的时间
    private boolean autoClose = true;//是否自动关闭

    public ZMUITipDialog(@NonNull Context context) {
        this(context, 0);
    }

    public ZMUITipDialog(@NonNull Context context, int themeResId) {
        this(context, themeResId, R.attr.zmui_style_ZMUITipDialog, R.style.zmui_style_ZMUITipDialog);

    }

    protected ZMUITipDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        setAttributes(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZMUITipDialog, defStyleAttr, defStyleRes);
        iconSize = a.getDimensionPixelSize(R.styleable.ZMUITipDialog_zmui_iconSize, 100);
        textSize = a.getDimension(R.styleable.ZMUITipDialog_zmui_textSize, -1);
        textColor = a.getColor(R.styleable.ZMUITipDialog_zmui_textColor, Color.WHITE);
        background = a.getResourceId(R.styleable.ZMUITipDialog_zmui_background, R.drawable.zmui_tip_dialog_bg);
        paddingH = a.getDimensionPixelSize(R.styleable.ZMUITipDialog_zmui_padding_horizontal, 100);
        paddingV = a.getDimensionPixelSize(R.styleable.ZMUITipDialog_zmui_padding_vertical, 50);

        a.recycle();

        if (DEBUG) {
            LogUtils.d(TAG, "initialize:iconSize=" + iconSize);
            LogUtils.d(TAG, "initialize:textSize=" + textSize);
            LogUtils.d(TAG, "initialize:textColor=" + Integer.toHexString(textColor));
            LogUtils.d(TAG, "initialize:background=" + background);
            LogUtils.d(TAG, "initialize:paddingH=" + paddingH);
            LogUtils.d(TAG, "initialize:paddingV=" + paddingV);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zmui_tip_dialog;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        mTipContent = findViewById(R.id.tip_content);
        mTipContent.setGravity(Gravity.CENTER_HORIZONTAL);
        mTipContent.setPadding(paddingH, paddingV, paddingH, paddingV);
        if (type == TYPE_LOADING) {
            ZMUILoadView zmuiLoadView = new ZMUILoadView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(iconSize, iconSize);
            zmuiLoadView.setLayoutParams(params);
            mTipContent.addView(zmuiLoadView);
        } else {
            ImageView imageView = null;
            if (type == TYPE_SUCCESS) {
                imageView = new ImageView(getContext());
                imageView.setImageResource(R.drawable.zmui_icon_tip_ok);
            } else if (type == TYPE_ERROR) {
                imageView = new ImageView(getContext());
                imageView.setImageResource(R.drawable.zmui_icon_tip_error);
            } else if (type == TYPE_INFO) {
                imageView = new ImageView(getContext());
                imageView.setImageResource(R.drawable.zmui_icon_tip_info);
            }
            if (imageView != null) {
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(iconSize, iconSize);
                imageView.setLayoutParams(params);
                mTipContent.addView(imageView);
            }
        }

        if (info != null) {
            TextView textView = new TextView(getContext());
            ZMUIUtils.setTextSize(textView, textSize);
            ZMUIUtils.setTextColor(textView, textColor);
            textView.setText(info);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (mTipContent.getChildCount() > 0) {
                params.topMargin = 20;
            }
            textView.setLayoutParams(params);
            mTipContent.addView(textView);
        }

        setOverlay(false);
        setWindowBackground();
    }

    private void setWindowBackground() {
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(background != 0 ? background : R.drawable.zmui_tip_dialog_bg);
        }
    }


    @Override
    public void show() {
        if (autoClose && showLength > 0) {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    dismiss();
                }
            }, showLength);
        }
        super.show();
    }

    public ZMUITipDialog setType(int type) {
        this.type = type;
        return this;
    }

    public ZMUITipDialog setInfo(String info) {
        this.info = info;
        return this;
    }

    public ZMUITipDialog setShowLength(int showLength) {
        this.showLength = showLength;
        return this;
    }

    public ZMUITipDialog setAutoClose(boolean autoClose) {
        this.autoClose = autoClose;
        return this;
    }

}
